/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.entitygen.method;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.apiGenerator.CefApiGeneratorContext;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.EntityGeneratorContext;
import com.inspur.edp.cef.generator.coreGenerator.base.mgrGenerator.method.GetModelResInfoGenerator;
import com.inspur.edp.cef.spi.entity.resourceInfo.builinImpls.CefEntityResInfoImpl;
import com.inspur.edp.cef.spi.entity.resourceInfo.builinImpls.CefValueObjResInfo;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import java.util.ArrayList;

import org.eclipse.jdt.core.dom.*;

public class GetResInfoGenerator extends ClassMethodGenerator {

  protected String fieldName;
  protected String nodeCode;
  protected EntityGeneratorContext parentContext;
  protected IGspCommonDataType dataType;

  public GetResInfoGenerator(String fieldName, IGspCommonDataType node,
      EntityGeneratorContext parentContext) {
    this.fieldName = fieldName;
    this.nodeCode = node.getCode();
    this.dataType = node;
    this.parentContext = parentContext;
  }

  @Deprecated
  public GetResInfoGenerator(String fieldName, String nodeCode,
      EntityGeneratorContext parentContext) {
    this.fieldName = fieldName;
    this.nodeCode = nodeCode;
    this.parentContext = parentContext;
  }

  public static final String MethodName = "getResourceInfo";

  @Override
  protected String getMethodName() {
    return MethodName;
  }

  @Override
  protected TypeInfo getReturnType() {
    return parentContext.getNode() instanceof IGspCommonObject ?
        new TypeInfo(CefEntityResInfoImpl.class) : new TypeInfo(CefValueObjResInfo.class);
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
    list.add(Modifier.ModifierKeyword.STATIC_KEYWORD);
    return list;
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    return new ArrayList<>();
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();

    String mgrName =  parentContext.getProjContext().getMgrImplType().getClassName();
    MethodInvocation mgrInvocation = ExpressionUtils.getMethodInvocation(ast, mgrName, GetModelResInfoGenerator.MethodName);
    MethodInvocation invocation = ExpressionUtils.getMethodInvocation(ast, mgrInvocation, "getCustomResource");
    invocation.arguments().add(ExpressionUtils.getStringLiteral(ast, nodeCode));

    CastExpression castExpression = ast.newCastExpression();
    castExpression.setType(getReturnType().getType(ast, parentContext.getProjContext()));
    castExpression.setExpression(invocation);

    ReturnStatement statement = ast.newReturnStatement();
    statement.setExpression(castExpression);

    block.statements().add(statement);

//    IfStatement ifStatement = ast.newIfStatement();
//    buildIfStatement(ifStatement);
//    buildThenStatement(ifStatement);
//    block.statements().add(ifStatement);
//
//    block.statements().add(buildReturnStatement());

    return block;
  }

  private void buildIfStatement(IfStatement ifStatement) {
    InfixExpression inifExpression = ast.newInfixExpression();
    inifExpression.setLeftOperand(ast.newSimpleName(fieldName));
    inifExpression.setOperator(InfixExpression.Operator.EQUALS);
    inifExpression.setRightOperand(ast.newNullLiteral());
    ifStatement.setExpression(inifExpression);
  }

  protected void buildThenStatement(IfStatement ifStatement) {

    Assignment assignment = ast.newAssignment();
    assignment.setOperator(Assignment.Operator.ASSIGN);
    assignment.setLeftHandSide(ast.newSimpleName(fieldName));
    ClassInstanceCreation creation = ast.newClassInstanceCreation();
    MediateType entityType = parentContext.getProjContext().getCefGenContext()
        .getEntityResourceInfos().get(nodeCode);
    String typeName = entityType.getFullName();
    TypeInfo type = new TypeInfo(typeName);
    creation.setType(type.getType(ast, parentContext.getProjContext()));
    if(dataType != null && dataType instanceof IGspCommonObject) {
      ClassInstanceCreation modelCreation = ast.newClassInstanceCreation();
      modelCreation.setType(ast.newSimpleType(ast.newName(
          ((CefApiGeneratorContext) parentContext.getProjContext().getCefGenContext()
              .getApiGenContext()).getMgrInfoType().getFullName())));
      creation.arguments().add(modelCreation);
    }
    assignment.setRightHandSide(creation);
    ExpressionStatement expressionStatement = ast.newExpressionStatement(assignment);

    ifStatement.setThenStatement(expressionStatement);
  }

  private ReturnStatement buildReturnStatement() {
    ReturnStatement statement = ast.newReturnStatement();
    statement.setExpression(ast.newSimpleName(fieldName));
    return statement;
  }
}
